/*
 * Copyright (C) 2004, Velocityme Partnership
 * MenuButtonAction.java
 *
 * Created on 06 September 2004, 08:34
 */

package com.velocityme.www.interfaces;

//web imports
import com.velocityme.www.dto.*;
import com.velocityme.www.actionforms.*;
import com.velocityme.www.utility.NodeUtil;
import org.apache.struts.action.*;
import org.apache.log4j.Logger;
import javax.servlet.http.*;
import java.util.*;

import org.apache.commons.beanutils.*;
//ejb interfaces
import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
/**
 *
 * @author  Robert
 */
public class MenuButtonAction extends org.apache.struts.action.Action {
    
    private static final Logger m_log = Logger.getLogger(MenuButtonAction.class);

    /** Creates a new instance of MenuButtonAction */
    public MenuButtonAction() {
    }
    
    public ActionForward execute(ActionMapping mapping, 
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {
    
        ButtonActionForm buttonActionForm = (ButtonActionForm)form;
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("buttonValues", null);
        if (buttonActionForm.getButton().equals("file-home")) {
            return mapping.findForward("ACTION_FILE_HOME_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("file-logout")) {
            // make sure we don't jump straight back to this node on login
            session.removeAttribute("nodePK");
            if (key != null) {
                key.logout();
                key.remove();
            }
            session.invalidate();
            return mapping.findForward("ACTION_FILE_LOGOUT_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-tasks-all-responsible")) {
            session.setAttribute("resultValues", cs.findAllMyResponsibleTasks(key));
            return mapping.findForward("ACTION_ALL_MY_RESPONSIBLE_TASKS_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-tasks-all-interested")) {
            session.setAttribute("resultValues", cs.findAllMyInterestedTasks(key));
            return mapping.findForward("ACTION_ALL_MY_INTERESTED_TASKS_SUCCESS");
        }
        if (buttonActionForm.getButton().startsWith("bookmark-nodeId-")) {
            int bookmarkNodeId = Integer.parseInt(buttonActionForm.getButton().substring(16));
            NodePK bookmarkNodePK = new NodePK(new Integer(bookmarkNodeId));
            request.setAttribute("nodePK", bookmarkNodePK);
            return mapping.findForward("ACTION_BOOKMARK_SHOW");
        }
        if (buttonActionForm.getButton().equals("help-content")) {
            return mapping.findForward("ACTION_HELP_CONTENT_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("help-about")) {
            session.setAttribute("version", key.getServerImplementationVersion());
            return mapping.findForward("ACTION_HELP_ABOUT_SUCCESS");
        }
        Integer nodeId = (Integer)session.getAttribute("nodeId");
        m_log.info("nodeId = " + nodeId.toString());
        NodePK nodePK = null;
        NodeValue nodeValue = null;
        if (nodeId != null && nodeId.intValue() != 0) {
            nodePK = new NodePK(nodeId);
            request.setAttribute("nodePK", nodePK);
            nodeValue = cs.getNodeValue(key, nodePK);
            session.setAttribute("nodeValue", nodeValue);
        }
        if (buttonActionForm.getButton().equals("bookmark-show") || buttonActionForm.getButton().equals("bookmark-hide")) {
            cs.setShowBookmarks(key, new Boolean(buttonActionForm.getButton().equals("bookmark-show")));
            if (nodeId != null && nodeId.intValue() != 0)
                return mapping.findForward("ACTION_SUCCESS");
            else
                return mapping.findForward("ACTION_FILE_HOME_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("edit-node-mark")) {
            session.setAttribute("markedNodePK", nodePK);
            return mapping.findForward("ACTION_MARK_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("edit-node-move")) {
            NodePK childPK = (NodePK)session.getAttribute("markedNodePK");
            cs.moveChildNode(key, nodePK, childPK);
            session.removeAttribute("markedNodePK");
            return mapping.findForward("ACTION_MOVE_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("edit-node-delete")) {
            return mapping.findForward("ACTION_DELETE_CHECK");
        }
        if (buttonActionForm.getButton().equals("edit-node-link")) {
            NodePK childPK = (NodePK)session.getAttribute("markedNodePK");
            cs.createNodeLink(key, nodePK, childPK);
            session.removeAttribute("markedNodePK");
            return mapping.findForward("ACTION_LINK_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("edit-node-dependency")) {
            NodePK dependencyPK = (NodePK)session.getAttribute("markedNodePK");
            cs.createTaskDependency(key, dependencyPK, nodePK);
            session.removeAttribute("markedNodePK");
            return mapping.findForward("ACTION_DEPENDENCY_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("edit-node-dependant")) {
            NodePK dependencyPK = (NodePK)session.getAttribute("markedNodePK");
            cs.createTaskDependency(key, nodePK, dependencyPK);
            session.removeAttribute("markedNodePK");
            return mapping.findForward("ACTION_DEPENDANT_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-node")) {
            return mapping.findForward("ACTION_CREATE_NODE_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-sprint")) {
            return mapping.findForward("ACTION_CREATE_SPRINT_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-project")) {
            return mapping.findForward("ACTION_CREATE_PROJECT_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-anchor")) {
            return mapping.findForward("ACTION_CREATE_ANCHOR_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-action")) {
            return mapping.findForward("ACTION_CREATE_ACTION_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-issue")) {
            return mapping.findForward("ACTION_CREATE_ISSUE_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-organisation")) {
            return mapping.findForward("ACTION_CREATE_ORGANISATION_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-department")) {
            return mapping.findForward("ACTION_CREATE_DEPARTMENT_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-person")) {
            return mapping.findForward("ACTION_CREATE_PERSON_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-user")) {
            return mapping.findForward("ACTION_CREATE_USER_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-group")) {
            return mapping.findForward("ACTION_CREATE_GROUP_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-state")) {
            return mapping.findForward("ACTION_CREATE_STATE_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-status")) {
            return mapping.findForward("ACTION_CREATE_STATUS_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-task-type")) {
            return mapping.findForward("ACTION_CREATE_TASKSTATEMACHINE_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-role")) {
            return mapping.findForward("ACTION_CREATE_ROLE_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("create-user-access")) {
            session.setAttribute("nodePathName", cs.getNodePathName(key, nodePK));
            session.setAttribute("userPairs", NodeUtil.getAllUserPairs(key, cs));
            session.setAttribute("rolePairs", NodeUtil.getAllRolePairs(key, cs));
            return mapping.findForward("ACTION_CREATE_USER_ACCESS_SUCCESS");
        }            
        if (buttonActionForm.getButton().equals("create-group-access")) {
            session.setAttribute("nodePathName", cs.getNodePathName(key, nodePK));
            session.setAttribute("groupPairs", NodeUtil.getAllGroupPairs(key, cs));
            session.setAttribute("rolePairs", NodeUtil.getAllRolePairs(key, cs));
            return mapping.findForward("ACTION_CREATE_GROUP_ACCESS_SUCCESS");
        }            
        if (buttonActionForm.getButton().equals("search-tasks-my-responsible")) {
            session.setAttribute("nodeValue", nodeValue);
            session.setAttribute("resultValues", cs.findMyResponsibleTasks(key, nodePK));
            return mapping.findForward("ACTION_MY_RESPONSIBLE_TASKS_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-tasks-my-interested")) {
            session.setAttribute("nodeValue", nodeValue);
            session.setAttribute("resultValues", cs.findMyInterestedTasks(key, nodePK));
            return mapping.findForward("ACTION_MY_INTERESTED_TASKS_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-tasks-others-responsible")) {
            return mapping.findForward("ACTION_OTHERS_RESPONSIBLE_TASKS_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-tasks-new")) {
            return mapping.findForward("ACTION_NEW_TASKS_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-tasks-active")) {
            session.setAttribute("nodeValue", nodeValue);
            session.setAttribute("resultValues", cs.findActiveTasks(key, nodePK));
            return mapping.findForward("ACTION_ACTIVE_TASKS_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-tasks-completed")) {
            return mapping.findForward("ACTION_COMPLETED_TASKS_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-tasks-changed")) {
            return mapping.findForward("ACTION_CHANGED_TASKS_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-tasks-advanced")) {
            return mapping.findForward("ACTION_ADVANCED_SEARCH_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("search-time")) {
            return mapping.findForward("ACTION_TIME_REPORT_SUCCESS");
        }
        if (buttonActionForm.getButton().equals("bookmark-add-node")) {
            return mapping.findForward("ACTION_BOOKMARK_ADD_NODE");
        }
        if (buttonActionForm.getButton().equals("bookmark-remove-node")) {
            return mapping.findForward("ACTION_BOOKMARK_REMOVE_NODE");
        }
        return mapping.findForward("ACTION_FAILED");
    }
    
}
